package servlet;

import bean.HouseInfo;
import utils.DBUtil;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

@WebServlet("/list")
public class ListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        1. 编写sql语句 使用JSP显示
        String sql = "select * from house_info i,house_type t where i.house_type_id = t.id";
//        2. 将sql语句 传给工具类
        ResultSet re = DBUtil.query(sql);
//        3. 设置一个集合，遍历结果集，封装成对象 再将对象添加到集合中
        ArrayList<HouseInfo> list = new ArrayList<>();
        try {
            while (re.next()){
                int id = re.getInt(1);
                String leaseMode = re.getString(2);
                double rent = re.getDouble(3);
                String contacts = re.getString(4);
                String method = re.getString(5);
                int typeId = re.getInt(6);
                String address = re.getString(7);
                String type = re.getString(9);
                HouseInfo house = new HouseInfo(id, leaseMode, rent, contacts, method, typeId, address, type);
                list.add(house);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
//        4. 把集合添加request请求域中
        request.setAttribute("list",list);
//        5. 将请求转发给jSP
        request.getRequestDispatcher("/WEB-INF/list.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
